package com.yunze.web.controller.yunze.fhgl;

import com.alibaba.fastjson.JSON;
import com.yunze.common.annotation.Log;
import com.yunze.common.core.controller.BaseController;
import com.yunze.common.core.domain.entity.SysUser;
import com.yunze.common.core.domain.model.LoginUser;
import com.yunze.common.enums.BusinessType;
import com.yunze.common.utils.ServletUtils;
import com.yunze.common.utils.ip.IpUtils;
import com.yunze.common.utils.spring.SpringUtils;
import com.yunze.common.utils.yunze.AesEncryptUtil;
import com.yunze.common.utils.yunze.Different;
import com.yunze.framework.web.service.TokenService;
import com.yunze.system.service.yunze.fhgl.IFhglExpressInquiryService;
import com.yunze.system.service.yunze.xsgl.customer.IYzXsglCustomerService;
import com.yunze.web.core.config.MyBaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发货管理 快递查询
 */
@RestController
@RequestMapping("/yunze/ExpressInquiry")
public class FhglExpressInquiryController extends MyBaseController
{

    @Resource
    private IFhglExpressInquiryService iFhglExpressInquiryService;
    @Resource
    private IYzXsglCustomerService iYzXsglCustomerService;


    /**
     * 快递查询 列表
     */
    @PreAuthorize("@ss.hasPermi('yunze:ExpressInquiry:list')")
    @PostMapping(value = "/list", produces = { "application/json;charset=UTF-8" })
    public String list(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));

            try {
                //获取对当前登录用户 菜单根目录 允许 btn 权限
                LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
                SysUser User = loginUser.getUser();
                HashMap<String, Object> AuthorityMap = new HashMap<String, Object>();
                AuthorityMap.put("user_id",User.getUserId());
                List<Map<String,Object>> UserArr = iYzXsglCustomerService.findSalesPartner(AuthorityMap);
                Object Btns = (UserArr.get(0)).get("Btns");
                if(Btns!=null && Btns.toString().length()>0){
                    String arr[] = Btns.toString().split(",");
                    if(Different.Is_existence(arr,"yunze:sys:Sales")){// 销售仅可看自己数据 【是否为销售业务员】
                        List<String> sales_idArr = new ArrayList<>();
                        sales_idArr.add(""+User.getUserId());
                        Parammap.put("sales_idArr",sales_idArr);
                    }
                }
            }catch (Exception e){
                System.out.println("仅可看所属销售 权限过滤操作失败");
            }

            return MyRetunSuccess(iFhglExpressInquiryService.selMap(Parammap),null);
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ExpressInquiry/list  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("获取快递查询 列表 操作失败！");
    }



    /**
     * （发货申请登记）快递查询 新增
     */
    @Log(title = "快递查询", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('yunze:ShippingApplication:Register')")
    @PostMapping(value = "/Register", produces = { "application/json;charset=UTF-8" })
    public String Register(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            boolean bool = iFhglExpressInquiryService.add_EIY(Parammap);
            if(bool){
                return MyRetunSuccess("操作成功！",null);
            }else{
                return Myerr("网络操作失败请稍后重试！");
            }
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ExpressInquiry/Register  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("新增 发货申请登记 操作失败！");
    }





    /**
     * 快递查询 详情
     */
    @PreAuthorize("@ss.hasPermi('yunze:ExpressInquiry:find')")
    @PostMapping(value = "/FindInfo", produces = { "application/json;charset=UTF-8" })
    public String FindInfo(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            return MyRetunSuccess(iFhglExpressInquiryService.find_data(Parammap),null);
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ExpressInquiry/FindInfo  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("快递查询 详情 操作失败！");
    }




    /**
     *  快递查询 修改
     */
    @Log(title = "快递查询", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('yunze:ExpressInquiry:upd')")
    @PostMapping(value = "/UpdInfo", produces = { "application/json;charset=UTF-8" })
    public String UpdInfo(@RequestBody String Pstr)
    {
        HashMap<String, Object> Parammap = new HashMap<String, Object>();
        if(Pstr!=null){
            Pstr = Pstr.replace("%2F", "/");//转义 /
        }
        try {
            Pstr =  AesEncryptUtil.desEncrypt(Pstr);
            Parammap.putAll(JSON.parseObject((String) Pstr));
            boolean bool = iFhglExpressInquiryService.upd_EIY_data(Parammap);
            if(bool){
                return MyRetunSuccess("操作成功！",null);
            }else{
                return Myerr("网络操作失败请稍后重试！");
            }
        }catch (Exception e){
            String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
            logger.error("<br/>  ExpressInquiry/UpdInfo  <br/> Pstr = " + Pstr + " <br/> ip =  " + ip + " <br/> ",e.getCause().toString());
        }
        return Myerr("快递查询 修改 操作失败！");
    }





}
